#!/usr/bin/env python
# coding=utf-8
#
#  Created by Skiloop
#  Email: skiloop@126.com
#
import cv2
import sys


def color_status(img):
    colors = {}
    h, w, d = img.shape
    for i in range(0, w):
        for j in range(0, h):
            color = str((img[j, i, 2], img[j, i, 1], img[j, i, 0]))
            if color not in colors:
                colors[color] = []
            colors[color].append(str((j, i)))
    return colors


def make_white(img):
    h, w, d = img.shape
    for i in range(0, w):
        for j in range(0, h):
            if img[j, i, 0] >= 200 and img[j, i, 1] >= 200 and img[j, i, 2] >= 200:
                img[j, i, 0] = 255
                img[j, i, 1] = 255
                img[j, i, 2] = 255


def cst(ifs, ofs):
    img = cv2.imread(ifs)
    make_white(img)
    cs = color_status(img)
    if ofs is not None:
        out = open(ofs, 'w')
    else:
        out = None
    for c, v in cs.items():
        print c, '==>', len(v)
        if out is not None:
            out.write(c)
            out.write(':')
            for idx in v:
                out.write(idx)
                out.write(',')
            out.write('\n')


if __name__ == '__main__':
    if len(sys.argv) < 2:
        print 'usage: %s input [output]' % __file__
    else:
        if len(sys.argv) == 2:
            out = None
        else:
            out = sys.argv[2]
            cst(sys.argv[1], out)
